﻿layui.use(['form', 'layedit', 'laydate', 'element'], function () {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , element = layui.element
        , laydate = layui.laydate;
    //自定义验证规则
    form.verify({
        yz: function (value) {
            if (value.length < 1) {
                return '至少输入一个字符';
            }
        }
    });
    var jsondata = { "title": "newFlow_1", "nodes": {}, "lines": {}, "areas": {}, "initNum": 1 }

    var property = {
        width: 1072,
        height: 600,
        toolBtns: ["start round", "end round", "task round", "node", "chat", "state", "plug", "join", "fork", "complex mix"],
        haveHead: true,
        headBtns: ["new", "open", "save", "undo", "redo", "reload"],//如果haveHead=true，则定义HEAD区的按钮
        haveTool: true,
        haveGroup: true,
        useOperStack: true
    };
    var remark = {
        cursor: "选择指针",
        direct: "结点连线",
        start: "入口结点",
        "end": "结束结点",
        "task": "任务结点",
        node: "自动结点",
        chat: "决策结点",
        state: "状态结点",
        plug: "附加插件",
        fork: "分支结点",
        "join": "联合结点",
        "complex mix": "复合结点",
        group: "组织划分框编辑开关"
    };
    var FlowMsg;
    $(document).ready(function () {
        FlowMsg = $.createGooFlow($("#FlowId"), property);
        FlowMsg.setNodeRemarks(remark);
        FlowMsg.onItemDel = function (id, type) {
            //if (confirm("确定要删除该单元吗?")) {
            this.blurItem();
            return true;
            //} else {
            //    return false;
            //}
        }
        var obj;
        FlowMsg.loadData(jsondata);
        FlowMsg.onItemFocus = function (id, model) {
            $("#NodeType").val(model);
            $("#NodeId").val(id);
            if (model == "line") {
                obj = this.$lineData[id];
                $("#NodeStatu").val(obj.M);
                $("#ele_left").val("");
            } else if (model == "node") {
                obj = this.$nodeData[id];
                $("#NodeStatu").val(obj.type);
                $("#FlowName").val(obj.setNodeApproval);
            }
            $("#NodeNames").val(obj.name);
            var nodeStatu = $("#NodeStatu").val().replace(/\s+/g, "");
            if (nodeStatu !== "startround" && nodeStatu !== "endround") {
                $("#ApprovalShow").show();
                $("select").attr("lay-verify","yz");
            }
            else
            {
                $("#ApprovalShow").hide();
                $("select").removeAttr("lay-verify");
            }
            return true;
        };
        FlowMsg.onItemBlur = function (id, model) {
            document.getElementById("propertyForm").reset();
            return true;
        };
        FlowMsg.onBtnNewClick = function () {
            FlowMsg.clearData();
        }
    });
    //监听提交
    form.on('submit(submit)', function (data) {
        var statu = $("#NodeStatu").val();
        data.field.ApprovalStatu = $("#ApprovalStatu").val();
        var NodeStatu = statu.replace(/\s+/g, "");
        if (FlowMsg.$focus === null || FlowMsg.$focus === "") {
            layer.alert("请选择一个节点");
            return;
        } else {
            FlowMsg.setName(FlowMsg.$focus, data.field.NodeNames, data.field.NodeType, JSON.stringify(data.field), NodeStatu);
            return;
        }
    });
    form.on('select(formId)', function (data) {
        $.getJSON("/Form/GetFormId?id=" + $("#FormsId").val(), function (data) {
            $("#content").html(data.data[0]["Content"]);
        });
    });
    //监听提交
    form.on('submit(submit2)', function (data) {
        var content = JSON.stringify(FlowMsg.exportData());
        $.ajax({
            url: '/Flow/AddFlowMsg',
            type: 'post',
            dataType: 'json',
            data: {
                Name: data.field.FlowName, FormId: data.field.Formid, TypeId: data.field.TypeId, Content: content
            },
            success: function (d) {
                if (d.code === 0) {
                    parent.alertMsy();
                }
                else {
                    layer.msg("失败", {
                        time: 2000 //20s后自动关闭
                        , icon: 5
                    });
                }
            }
        });
        var index = parent.layer.getFrameIndex(window.name); // 得到索引
        parent.layer.close(index);
    });
});